package com.my.bld.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.File;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.my.bld.model.User;
import com.my.bld.service.UserService;
import com.my.bld.utils.ExportIntentionUtils;
import com.my.bld.utils.Pager;
import com.my.bld.utils.UserUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.*;

/**
 * @ClassName:  
 * @Description: 
 * @author administrator
 * @date 2015年12月24日 下午1:46:33 - 2016年11月26日 21时06分03秒
 */

@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
	
	private static final long serialVersionUID = 1L;

	//==========model==============
	  private User user;
		@Override
		public User getModel() {
			if(user==null) user = new User();
			return user;	
		}
		//==========model==============
	/**
	 * 依赖注入 start dao/service/===
	 */
	@Autowired
	private UserService userService;

	
	//依赖注入 end  dao/service/===
	
	//-------------------------华丽分割线---------------------------------------------
	
	//============自定义参数start=============
	
	//============自定义参数end=============
	
	//-------------------------华丽分割线---------------------------------------------
	
	//============文件上传start=======================================================
	private File file;
	//提交过来的file的名字
    private String fileFileName;
    //提交过来的file的MIME类型
    private String fileContentType;
    public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	 //============文件上传end=========================================================
			
	 //-------------------------华丽分割线---------------------------------------------//
	
	 //=============公=======共=======方=======法==========区=========start============//
	/**
	 * 列表分页查询
	 */
	public String user(){
	    Map<String,Object> alias = new HashMap<String,Object>();
		StringBuffer sb = new StringBuffer();
		sb = sb.append("from User where 1=1 and isDelete = 0 ");
		if(user!=null && user.getRealName() !=null && !"".equals(user.getRealName())){
			sb.append("  and realName like :realName ");
			alias.put("realName", "%" +user.getRealName()+ "%" );
		}
		sb = sb.append("order by id desc");
		Pager<User> pagers = userService.findByAlias(sb.toString(),alias);
		ActionContext.getContext().put("pagers", pagers);
		ActionContext.getContext().put("User", user);
		return SUCCESS;
    }
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * 执行添加
	 * @return
	 */
	public String exAdd(){
		user.setCreateTime(new Date());
		user.setIsDelete(0);
		userService.save(user);
		ActionContext.getContext().put("url", "/user_user.do");
		return "redirect";
	}
	
	/**
	 * 查看详情页面
	 * @return
	 */
	public String view(){
		if(UserUtils.getLoginId() == null){
			return "uLogin";
		}
		User n = userService.getById(UserUtils.getLoginId());
		ActionContext.getContext().put("User", n);
		return SUCCESS;
	}
	public String view2(){
		if(UserUtils.getLoginId() == null){
			return "uLogin";
		}
		User n = userService.getById(UserUtils.getLoginId());
		ActionContext.getContext().put("User", n);
		return SUCCESS;
	}
	
	/**
	 * 跳转修改页面
	 * @return
	 */
	public String update(){
		User n = userService.getById(user.getId());
		ActionContext.getContext().put("User", n);
		return SUCCESS;
	}
	public String update2(){
		if(UserUtils.getLoginId() == null){
			return "manageLogin";
		}else{
			User n = userService.getById(UserUtils.getLoginId());
			ActionContext.getContext().put("User", n);
			return SUCCESS;
		}
		
	}
	/**
	 * 执行修改
	 * @return
	 */
	public String exUpdate(){
		User n = userService.getById(user.getId());
		n.setPassWord(user.getPassWord());
		n.setPhone(user.getPhone());
		n.setRealName(user.getRealName());
		n.setUserName(user.getUserName());
		userService.update(n);
		ActionContext.getContext().put("url", "/user_user.do");
		return "redirect";
	}
	public String exUpdate2(){
		User n = userService.getById(user.getId());
		n.setPassWord(user.getPassWord());
		n.setPhone(user.getPhone());
		n.setRealName(user.getRealName());
		n.setUserName(user.getUserName());
		userService.update(n);
		ActionContext.getContext().put("url", "/user_update2.do");
		return "redirect";
	}
	
	public String exUpdate3(){
		User n = userService.getById(user.getId());
		n.setPassWord(user.getPassWord());
		n.setPhone(user.getPhone());
		n.setRealName(user.getRealName());
		n.setUserName(user.getUserName());
		userService.update(n);
		return "uLogin";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		User n = userService.getById(user.getId());
		n.setIsDelete(1);
		userService.update(n);
		ActionContext.getContext().put("url", "/user_user.do");
		return "redirect";
	}
	
	//=============公=======共=======方=======法==========区=========end============//
	
	 //-------------------------华丽分割线---------------------------------------------//
	
	 //=============自=======定=======义=========方=======法==========区=========start============//
	
	
	
	//=============自=======定=======义=========方=======法==========区=========end============//
	
	public void daochu(){
		  Map<String,Object> alias = new HashMap<String,Object>();
			StringBuffer sb = new StringBuffer();
			sb = sb.append("from User where 1=1 and isDelete = 0 ");
			if(user!=null && user.getRealName() !=null && !"".equals(user.getRealName())){
				sb.append("  and realName like :realName ");
				alias.put("realName", "%" +user.getRealName()+ "%" );
			}
			sb = sb.append("order by id desc");
			 List<User>  users = userService.listByAlias(sb.toString(), alias);
			 HSSFWorkbook wb = null;
				String filename = "销售员工";
				String name = "销售员";
				try {
					filename = java.net.URLEncoder.encode(filename, "UTF-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("application/vnd.ms-excel;");
				response.setHeader("Content-disposition", "attachment;filename="
						+ filename + ".xls");
				if(users.size()>0){
					/*private String userName;//登陆�?
					private String passWord;//密码
					private String phone;//手机�?
					private String realName;//真实�?
*/					wb = ExportIntentionUtils
							.ExportIntentionInfo(
									"登录名,密码,手机号,真实姓名",users, name);
					OutputStream ouputStream;
					try {
						ouputStream = response.getOutputStream();
						wb.write(ouputStream);
						ouputStream.flush();
						ouputStream.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
					
				}
				
				
	}
	
}
